package chapter03

import org.apache.log4j.{Level, Logger}
import org.apache.spark.{SparkConf, SparkContext}
object Test13_Filter {
  def main(args: Array[String]): Unit = {
    val logger = Logger.getLogger("org.apache.spark")
    logger.setLevel(Level.WARN)
    val conf = new SparkConf().setMaster("local[*]").setAppName("Filter")
    val sc = new SparkContext(conf)
    val value = sc.makeRDD(List(1, 2, 3, 4, 5, 6, 7, 8, 9))
    //进行过滤 将所有偶数保留 将奇数去掉
    println(value.filter(e => e % 2 == 0).collect().mkString("Array(", ", ", ")"))
    //练习 Hadoop HBase Spark Scala Java 保留首字母为H的单词
    val value1 = sc.makeRDD(List("Hadoop", "HBase", "Spark", "Scala", "Java"))
    println(value1.filter(e=>e(0).equals('H')).collect().toList)
    println(value1.filter(e=>e.startsWith("H")).collect().toList)
    //println(value1.filter(e=>e.matches()).collect().toList)
    //从服务器日志数据apache.log中获取2015年5月17日的请求路径
    val value2 = sc.textFile("input/apache.log")
    println(value2
      .map(e=>{
        val strings = e.split(" ")
        (strings(3).split(":")(0),strings(6))
      })
      .filter(e=>e._1.contains("17/05/2015"))
      .map(e=>e._2)
      .collect().mkString("Array(", ", ", ")"))
  }
}
